// CyTargetLinker,
// a Cytoscape plugin to extend biological networks with regulatory interaction
// Copyright 2011-2012 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cytargetlinker.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import cytargetlinker.CyTargetLinker;
import cytoscape.Cytoscape;

/**
 * dialog to select which files in the RIN directory
 * should be used
 *
 */
public class RINSelectionDialog extends JDialog {
	
	private CyTargetLinker linker;
	private Map<String, File> riNetworks;
	private Map<String, File> selectedFiles;
	
	private CellConstraints cc = new CellConstraints();
	
	public RINSelectionDialog(CyTargetLinker linker, Map<String, File> availMirnaNetworks) {
		super(Cytoscape.getDesktop());
		selectedFiles = new HashMap<String, File>();
		riNetworks = availMirnaNetworks;
		cbMap = new HashMap<String, JCheckBox>();
		
		this.linker = linker;
		this.add(getContent());
		locate();
		this.setResizable(false);
		this.pack();
	}
	
	private JPanel getContent() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BorderLayout(1,1));
		
		panel.add(getMainPanel(), BorderLayout.CENTER);
		panel.add(getCancelOkPanel(), BorderLayout.SOUTH);
		return panel;
	}

	private Component getMainPanel() {
		
		FormLayout layout = new FormLayout("10dlu, p, 5dlu, p, 10dlu", getRowLayout());
		PanelBuilder builder = new PanelBuilder(layout);
		
		builder.add(new JLabel("RIN Files"), cc.xy(2, 2));
		builder.addSeparator("", cc.xyw(2, 3, 3));
		
		int i = 0;
		for(String str : riNetworks.keySet()) {
			builder.add(new JLabel(riNetworks.get(str).getName()), cc.xy(2, 2*i+5));
			JCheckBox box = new JCheckBox();
			box.setSelected(true);
			builder.add(box, cc.xy(4, 2*i+5));
			cbMap.put(str, box);
			i++;
		}
		
		JScrollPane pane = new JScrollPane(builder.getPanel());
		return pane;
	}
	
	private Map<String, JCheckBox> cbMap;
	
	private String getRowLayout() {
		String str = "10dlu, p, 15dlu, 5dlu";
		for(int i = 0; i < riNetworks.size(); i++) {
			str = str + ",p,5dlu";
		}
		str = str + ",10dlu";
		return str;
	}
	
	private JPanel getCancelOkPanel() {
		FormLayout layout = new FormLayout("right:max(72dlu;p), 10dlu, 72dlu, 10dlu", "5dlu, p, 5dlu");
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setDefaultDialogBorder();
		JButton button1 = new JButton("Cancel");
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					dispose();
			}
		});
		builder.add(button1, cc.xy(1, 2));
		JButton button2 = new JButton("Ok");
		button2.addActionListener(new ActionListener(){
			public void  actionPerformed(ActionEvent e) {
				for(String str : cbMap.keySet()) {
					if(cbMap.get(str).isSelected()) {
						selectedFiles.put(str, riNetworks.get(str));
					}
				}
				dispose();
				linker.startMerge(selectedFiles);
			}
		});
		
		builder.add(button2, cc.xy(3, 2));
		return builder.getPanel();
	}


	private void locate() {
		int sizeX = 300;
		int sizeY = 300;
		this.setPreferredSize(new Dimension(sizeX, sizeY));

		Point cyLocation = Cytoscape.getDesktop().getLocation();
		int cyHeight = Cytoscape.getDesktop().getHeight();
		int cyWidth = Cytoscape.getDesktop().getWidth();

		Point middle = new Point(cyLocation.x + (cyWidth / 2), cyLocation.y + (cyHeight / 2));

		int locX = middle.x - (sizeX / 2);
		int locY = middle.y - (sizeY / 2);
		this.setLocation(new Point(locX, locY));
	}
}
